package com.starry.home.ui.login

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.starry.core.base.BaseActivity
import com.starry.core.constant.GlobalConfig
import com.starry.core.utils.SpUtil
import com.starry.core.utils.ZKStatusBarUtil
import com.starry.home.R
import com.starry.home.databinding.ActivityLoginBinding
import com.starry.home.logic.model.login.LoginBean
import com.starry.home.ui.main.MainActivity
import com.starry.home.ui.mine.UserInfoActivity
import com.starry.home.viewmodel.login.LoginViewModel

/**
 * Package:com.starry.dotpen_patients.ui.login
 * Author:starr
 * Time:2024/1/3 20:26
 * Description: 登录注册界面
 */
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {


    private var phone: String = ""
    private var password: String = ""
    private var passwordAgain: String = ""


    companion object {
        var isLogin: Boolean = false
        fun startAction(context: Context, bundle: Bundle) {
            val intent = Intent(context, LoginActivity::class.java)
            isLogin = bundle.getBoolean("isLogin")
            context.startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        if (isLogin) {
            mDataBinding.topLogin.setTextColor(getColor(R.color.black90))
            mDataBinding.topRigster.setTextColor(getColor(R.color.black10))
            mDataBinding.topRigster.textSize = (18f)
            mDataBinding.topRegisterLine.visibility = View.INVISIBLE
            mDataBinding.topLogin.textSize = (28f)
            mDataBinding.topLoginLine.visibility = View.VISIBLE
            mDataBinding.edtPasswordAgain.visibility = View.GONE
            mDataBinding.btLogin.text = "登录"
            isLogin = true
        } else {
            mDataBinding.topLogin.setTextColor(getColor(R.color.black10))
            mDataBinding.topRigster.setTextColor(getColor(R.color.black90))
            mDataBinding.topRigster.textSize = (28f)
            mDataBinding.topRegisterLine.visibility = View.VISIBLE
            mDataBinding.topLogin.textSize = (18f)
            mDataBinding.topLoginLine.visibility = View.INVISIBLE
            mDataBinding.edtPasswordAgain.visibility = View.VISIBLE
            mDataBinding.btLogin.text = "注册"
            isLogin = false
        }
    }

    @SuppressWarnings("all")
    inner class LoginClick {

        //游客登录
        fun touristLogin(view: View?) {
            MainActivity.startAction(getContext())
            finish()
        }

        //切换成注册
        fun registerSwitch(view: View?) {
            mDataBinding.topLogin.setTextColor(R.color.black10)
            mDataBinding.topRigster.setTextColor(R.color.black90)
            mDataBinding.topRigster.textSize = (28f)
            mDataBinding.topRegisterLine.visibility = View.VISIBLE
            mDataBinding.topLogin.textSize = (18f)
            mDataBinding.topLoginLine.visibility = View.INVISIBLE
            mDataBinding.edtPasswordAgain.visibility = View.VISIBLE
            mDataBinding.btLogin.text = "注册"
            isLogin = false
        }

        //切换成登录
        fun loginSwitch(view: View?) {
            mDataBinding.topLogin.setTextColor(R.color.black90)
            mDataBinding.topRigster.setTextColor(R.color.black10)
            mDataBinding.topRigster.textSize = (18f)
            mDataBinding.topRegisterLine.visibility = View.INVISIBLE
            mDataBinding.topLogin.textSize = (28f)
            mDataBinding.topLoginLine.visibility = View.VISIBLE
            mDataBinding.edtPasswordAgain.visibility = View.GONE
            mDataBinding.btLogin.text = "登录"
            isLogin = true
        }

        //登录
        fun login(view: View?) {
            phone = (mDataBinding.edtPhone.text.toString()).trim()
            password = (mDataBinding.edtPassword.text.toString()).trim()
            if (phone.isBlank() || password.isBlank()) {
                Toast.makeText(getContext(), "请输入手机号或密码", Toast.LENGTH_SHORT).show()
            }
            //登录
            if (isLogin) {
                mViewModel.login(LoginBean(phone, password))
            } else {
                passwordAgain = (mDataBinding.edtPasswordAgain.text.toString()).trim()
                if (phone.isBlank() || password.isBlank() || passwordAgain.isBlank()) {
                    Toast.makeText(getContext(), "请输入手机号或密码", Toast.LENGTH_SHORT)
                        .show()
                }
                if (password != passwordAgain) {
                    Toast.makeText(getContext(), "两次输入的密码不一致", Toast.LENGTH_SHORT)
                        .show()
                }
                //注册
                mViewModel.register(LoginBean(phone, password))
            }

        }


    }


    override fun initData() {

        mDataBinding.click = LoginClick()

    }

    override fun initView() {
        ZKStatusBarUtil.setTransparentForWindow(this)
        ZKStatusBarUtil.setDarkMode(this)
        mViewModel.loginResponseBean.observe(this@LoginActivity) {
            if (it.isSuccess) {
                //暂存账号和密码和用户id
                val result = it.getOrNull()
                SpUtil.setStringValue(GlobalConfig.userPhone, phone)
                SpUtil.setStringValue(GlobalConfig.userPassword, password)
                SpUtil.setIntValue(GlobalConfig.userId, result?.emrHzId!!)
                if (result.emrHzName.isBlank()) {
                    //完善个人信息
                    UserInfoActivity.startAction(getContext())
                    finish()
                } else {
                    //进入主界面
                    MainActivity.startAction(getContext())
                    finish()
                }
            } else {
                Toast.makeText(getContext(), it.exceptionOrNull()?.message, Toast.LENGTH_SHORT)
                    .show()
            }
        }

        mViewModel.registerBean.observe(this@LoginActivity) {
            if (it.isSuccess) {
                if (it.getOrNull() == null || it.getOrNull()?.emrHzId == null) {
                    Toast.makeText(getContext(), "注册失败", Toast.LENGTH_SHORT)
                        .show()
                    return@observe
                }
                Toast.makeText(getContext(), "注册成功", Toast.LENGTH_SHORT)
                    .show()
            } else {
                Toast.makeText(getContext(), it.exceptionOrNull()?.message, Toast.LENGTH_SHORT)
                    .show()
            }
        }
    }


    override fun getLayoutId() = R.layout.activity_login


}

